package com.chookss.home.gallery;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.chookss.R;
import com.chookss.tools.Utils;
import com.chookss.view.DrawableTextView;
import com.lxj.xpopup.core.DrawerPopupView;

/**
 * Description: 自定义抽屉弹窗
 * Create by dance, at 2018/12/20
 */
public class CustomDrawerPopupView extends DrawerPopupView {
    TextView tvReset, tvSure;
    DrawableTextView tvStartTime, tvEndTime, tvProvide, tvDirectory, tvLabel, tvLocation;
    LinearLayout llType2, llType1;
    TextView tvType0, tvType1, tvType2, tvType3, tvType4;
    LinearLayout llBottom;
    private CusTomDrawerPopupListener listener = null;

    public CustomDrawerPopupView(@NonNull Context context) {
        super(context);
    }

    public CustomDrawerPopupView(@io.reactivex.annotations.NonNull Context context, CusTomDrawerPopupListener listener) {
        super(context);
        this.listener = listener;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.custom_drawer_popup;
    }

    @Override
    protected void onCreate() {
        llBottom = findViewById(R.id.llBottom);
        tvStartTime = findViewById(R.id.tvStartTime);
        tvEndTime = findViewById(R.id.tvEndTime);
        tvProvide = findViewById(R.id.tvProvide);
        llType1 = findViewById(R.id.llType1);
        llType2 = findViewById(R.id.llType2);
        tvType0 = findViewById(R.id.tvType0);
        tvType1 = findViewById(R.id.tvType1);
        tvType2 = findViewById(R.id.tvType2);
        tvType3 = findViewById(R.id.tvType3);
        tvType4 = findViewById(R.id.tvType4);
        tvDirectory = findViewById(R.id.tvDirectory);
        tvLabel = findViewById(R.id.tvLabel);
        tvLocation = findViewById(R.id.tvLocation);
        tvReset = findViewById(R.id.tvReset);
        tvSure = findViewById(R.id.tvSure);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) llBottom.getLayoutParams();
        params.setMargins(0, 0, 0, Utils.getStatusBarHeight(getContext()));
        llBottom.setLayoutParams(params);

        tvType0.setOnClickListener(v -> {
            setType(0);
        });
        tvType1.setOnClickListener(v -> {
            setType(1);
        });
        tvType2.setOnClickListener(v -> {
            setType(2);
        });
        tvType3.setOnClickListener(v -> {
            setType(3);
        });
        tvType4.setOnClickListener(v -> {
            setType(4);
        });
        tvStartTime.setOnClickListener(v -> {
            if (listener != null) {
                listener.onListItemClick(0, "");
            }
        });
        tvEndTime.setOnClickListener(v -> {
            if (listener != null) {
                listener.onListItemClick(1, "");
            }
        });
        tvProvide.setOnClickListener(v -> {
            if (listener != null) {
                listener.onListItemClick(2, "");
            }
        });
        tvDirectory.setOnClickListener(v -> {
            if (listener != null) {
                listener.onListItemClick(3, "");
            }
        });
        tvLabel.setOnClickListener(v -> {
            if (listener != null) {
                listener.onListItemClick(4, "");
            }
        });
        tvLocation.setOnClickListener(v -> {
            if (listener != null) {
                listener.onListItemClick(5, "");
            }
        });
        tvReset.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                tvStartTime.setText("");
                tvEndTime.setText("");
                tvProvide.setText("");
                tvDirectory.setText("");
                tvLocation.setText("");
                tvLabel.setText("");
                setType(-1);
                if (listener != null) {
                    listener.onListItemClick(6, "");
                }
            }
        });
        tvSure.setOnClickListener(v -> {
            if (listener != null) {
                listener.onListItemClick(7, "");
            }
        });
    }

    @Override
    protected void onShow() {
        super.onShow();
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
    }

    public void setData(int type, String content) {
        if (type == 0) {
            tvStartTime.setText(content);
        } else if (type == 1) {
            tvEndTime.setText(content);
        } else if (type == 2) {
            tvProvide.setText(content);
        } else if (type == 3) {
            tvDirectory.setText(content);
        } else if (type == 4) {
            tvLabel.setText(content);
        } else if (type == 5) {
            tvLocation.setText(content);
        }
    }

    public void setType(int type) {
        if (type == -1) {
            tvType0.setSelected(false);
            tvType1.setSelected(false);
            tvType2.setSelected(false);
            tvType3.setSelected(false);
            tvType4.setSelected(false);
        } else if (type == 0) {
            tvType0.setSelected(true);
            tvType1.setSelected(false);
            tvType2.setSelected(false);
            tvType3.setSelected(false);
            tvType4.setSelected(false);
        } else {
            tvType0.setSelected(false);
            if (type > 2) {
                if (llType2.getChildAt(type - 3).isSelected()) {
                    llType2.getChildAt(type - 3).setSelected(false);
                } else {
                    llType2.getChildAt(type - 3).setSelected(true);
                }
            } else {
                if (llType1.getChildAt(type).isSelected()) {
                    llType1.getChildAt(type ).setSelected(false);
                } else {
                    llType1.getChildAt(type ).setSelected(true);
                }
            }
        }
        changeTypeData();
    }

    public void changeTypeData() {
        // 设置 Adapter
        String content = "";
        if (!tvType0.isSelected()) {
            for (int i = 0; i < 2; i++) {
                if (llType1.getChildAt(i + 1).isSelected()) {
                    if (Utils.isNull(content)) {
                        content = (i + 1) + "";
                    } else {
                        content = content + "," + (i + 1);
                    }
                }

            }
            for (int i = 0; i < 2; i++) {
                if (llType2.getChildAt(i).isSelected()) {
                    if (Utils.isNull(content)) {
                        content = (i + 3) + "";
                    } else {
                        content = content + "," + (i + 3);
                    }
                }

            }
        }
        if (listener != null) {
            listener.onListItemClick(8, content);
        }
    }
}


